package commands
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class CommandContainer extends Sprite
	{
		private var _command:ICommand;
		private var _x:Number;
		private var _y:Number;
		
		public function CommandContainer(command:ICommand,labelText:String,xValue:Number,yValue:Number)
		{
			_command = command;
			graphics.lineStyle(1);
			graphics.beginFill(0xffffff,1);
			graphics.drawRect(0,0,50,50);
			graphics.endFill();
			
			var label:TextField = new TextField;
			label.width = 50;
			label.height = 50;
			label.multiline = true;
			label.wordWrap = true;
			label.text = labelText;
			label.selectable = false;
			addChild(label);
			
			this.addEventListener(MouseEvent.MOUSE_DOWN,onMouseDown);
			this.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			_x = xValue;
			_y = yValue;
			
			x = _x;
			y = _y;
		}
		private function onMouseDown(event:MouseEvent):void{
			this.startDrag();
		}
		private function onMouseUp(event:MouseEvent):void{
			this.stopDrag();
			x = _x;
			y = _y;
			
			var target:DisplayObject = this.dropTarget;
			
			while(target != null && !(target is CommandButton) && target != root){
				target = target.parent;
			}
			
			if(target is CommandButton){
				CommandButton(target).command = _command;
			}
		}
		
	}
}